/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRI_SYSTEM_H_
#define TRI_SYSTEM_H_

#ifdef WIN32
#include <conio.h>
#endif

#include <dirent.h>
#include <cstdio>
#include <vector>
#include <string>
#include <stdlib.h>

#include "utils.h"

namespace tri {
namespace sys {

/**
 * Join path names. 
 * eg join("c:/fldr","app.exe") returns "c:/fldr/app.exe"
 * 
 * @param d Location 1
 * @param d Location 2
 */
std::string join(std::string d, std::string s);

/**
 * Returns true if the given file exists.
 * 
 * @param file File to validate
 */
bool isFile(std::string file);

/**
 * Returns true if the given directory exists.
 * 
 * @param path Directory to validate
 */
bool isDir(std::string path);

/**
 * Get a list of files in a given folder.
 * 
 * @param path Folder to search through
 * @param v Storage for file list. 
 * @return -1 if method failed. Use 
 *     tri::sys::isdir(path) to check that 
 *     the directory exists. 
 */
int getFiles(std::string path, std::vector<std::string>& v);

/**
 * Same as  getfiles(path,v), but takes an extra
 * list of file extensions to look for. Extensions not
 * in the list are not listed.
 * 
 * @param path Folder to search through
 * @param v Storage for file list. 
 * @param ext List of file extensions. 
 * @return -1 if method failed. Use 
 *     tri::sys::isdir(path) to check that 
 *     the directory exists. 
 */
int getFiles(std::string path, std::vector<std::string>& v,
        std::vector<std::string>& ext);

/**
 * Get a list of folders within a given folder.
 * 
 * @param path Folder to search through
 * @param v Storage for file list. 
 * @return -1 if method failed. Use 
 *     tri::sys::isdir(path) to check that 
 *     the directory exists. 
 */
int getDirs(std::string path, std::vector<std::string>& v);

/**
 * @see getdirs(path,v)
 */
int listDir(std::string path, std::vector<std::string>& v);

/**
 * Returns isfile(path) || isdir(path) 
 */
bool exists(std::string path);

/**
 * Get the applications current working directory.
 * 
 * @return Curretn working directory
 */
std::string getCWDir();

/**
 * Returns true is the filename ends with the given extension.
 */
bool hasExtension(std::string filename, std::string ext);

/**
 * Format the given path string
 * WIN32 : '/' is replaced by '\'   
 */
std::string formatPathString(std::string path);

/**
 * Returns true if the given path has a parent directory
 */
bool hasParentDirectory(std::string path);

/**
 * Returns the parent directory
 * If hasParentDirectory fails an empty string
 * is returned.
 */
std::string getParentDirectory(std::string path);

}
}

#endif /*SYSTEM_H_*/
